<?php

namespace app\shop\controller;

use buildView\Actions;
use buildView\Grid;
use think\Controller;
use buildView\Form;
use app\shop\model\ShopStoreSpending;
use buildView\Filter;
use think\Db;

/**
 * 门店开支管理
 * Class StoreSpending
 * @package app\admin\controller
 */
class StoreSpending extends Controller
{
    /**
     * 列表
     * @auth true
     * @menu true
     * @return string
     */
    public function index()
    {
        $store = session('store');
        $grid = new Grid(new ShopStoreSpending);
        $grid->setTitle('门店开支管理');
        // $grid->model()->order('sort asc,id desc');
        if ($store){
            $grid->model()->where('store_id',$store['id']);
        }
        $grid->column('store_id','店铺')->display(function ($val, $data){
             //查询商家
            $storeData = Db::name('shop_store')->field('id,name')->select();
            $storeData = array_column($storeData, 'name', 'id');
            return $storeData[$data['store_id']];
        })->width(200)->align('left');
        $grid->rent_gold('租金');
        $grid->wage('员工工资');
        $grid->daily_spending('日常开支');
        $grid->return_goods('退货');
        $grid->loss('损失');
        $grid->office('办公');
        $grid->repair_cost('维修费');
        $grid->other_spending('其他开支');
        $grid->create_at('创建时间');
        $grid->setFromOpen();
        $grid->filter(function(Filter $filter){
            $store = session('store');
            $storeData = Db::name('shop_store')->field('id,name')->select();
            $storeData = array_column($storeData, 'name', 'id');
            if (!$store){
                $filter->eq('store_id', '店铺')->select($storeData);
            }
            $filter->dateBetween('create_at','创建时间');
         });
        $grid->actions(function (Actions $action) {
            $action->hideDetail();
        });
        return $grid->view();
    }

    /**
     * 添加/编辑
     * @auth true
     * @menu true
     * @return string
     */
    public function form()
    {
        //查询商家
        $storeData = Db::name('shop_store')->field('id,name')->select();
        $storeData = array_column($storeData, 'name', 'id');
        $form = new Form(new ShopStoreSpending);
        $form->select('store_id', '店铺')->options($storeData);
        //查询店铺
        $form->text('rent_gold','租金');
        $form->text('wage','员工工资');
        $form->text('daily_spending','日常开支');
        $form->text('return_goods','退货');
        $form->text('loss','损失');
        $form->text('office','办公');
        $form->text('repair_cost','维修费');
        $form->text('other_spending','其他开支');
        return $form->view();
    }


}
